<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class Cms_Report extends Model
{
    use HasFactory;

    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'cms_report';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    protected $fillable = [
        'mid',
        'otherid',
        'reason',
        'status',
        'is_del'
    ];
    protected $appends = ['reason_name'];
    public function scopeNodel($query)
    {
        return $query->where('is_del', 0);
    }

    protected $hidden = [
        'is_del',
    ];

    public function getReasonNameAttribute()
    {
        $params = Sys_Params::where('itemcode','blacktype')->first();
        $itemvalue = json_decode($params->itemvalue,true);
        $reason_name = isset($itemvalue[$this->reason]) ? $itemvalue[$this->reason] : '暂无';
        return $reason_name;
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function hasOneMember()
    {
        return $this->hasOne('App\Models\Cms_Member', 'id', 'mid');
    }

    public function hasOneOtherMember()
    {
        return $this->hasOne('App\Models\Cms_Member', 'id', 'otherid');
    }

    public static function getFilter($filter)
    {
        return self::where(function ($query) use ($filter){
            if (isset($filter['status']) && !empty($filter['status'])) {
                $query->where('status',  $filter['status'] );
            }
        });
    }
}
